//################################################################################
// MIT License
// Copyright (c) 2024 ZhangYihua
//
// Change Logs:
// Date           Author       Notes
// 2023-02-22     ZhangYihua   first version
//
// Description  : 
//################################################################################

function integer ecc_cw_f;
    input integer dw;        // original data(before encode) width
    input [8*8-1:0] mode;   // "SEC_ONLY", "SEC_DED", "NONE"

    integer cw;
    begin
        if (mode=="SEC_ONLY") begin
            // DW <= 2^CW - CW - 1
            cw = 2;
            while(dw > ((1<<cw)-cw-1) ) begin
                cw = cw + 1;
            end
        end else if (mode=="SEC_DED") begin
            // DW <= 2^(CW-1) - CW
            // 2^(CW-1) = cmb_num_f(CW, 1) + cmb_num_f(CW, 3) + ... + cmb_num_f(CW, 2t+1)      (2t+1<=CW)
            cw = 3;
            while(dw > ((1<<(cw-1))-cw) ) begin
                cw = cw + 1;
            end
        end else begin
            cw = 0;
        end

        ecc_cw_f = cw;
    end
endfunction

